<template>
  <div class="weather-header">
    <nav class="container">
      <a href="/">
        <div class="title">
          <font-awesome-icon icon="fa-solid fa-sun" />
          <span>新中地天气</span>
        </div>
      </a>
      <div class="city">
        <h4>{{ weatherData.city }}</h4>
        <span>实时天气: {{ weatherData.weather }}{{ weatherData.temperature }}℃</span>
        <span>{{ weatherData.winddirection }}风{{ weatherData.windpower }}级</span>
      </div>
      <div class="icon">
        <font-awesome-icon icon="fa-solid fa-circle-info" @click="toggleInfo" />
        <font-awesome-icon
          icon="fa-solid fa-plus"
          :class="needAdd ? '' : 'add'"
        />
      </div>
    </nav>
    <div class="backdrop" :class="{ active: isShow }" @click="toggleInfo"></div>
    <div class="info-card" :class="isShow ? 'is-show' : ''">
      <h2>关于:</h2>
      <p>这个应用可以用来追踪你选择城市的当前天气</p>
      <h2>如何使用:</h2>
      <p>
        1.点击搜索框输入你希望追踪的城市
        <br />
        2.在搜索结果中选中一个城市，你将获取当地最新的天气
        <br />
        3.点击右侧的＋号可以将追踪城市的天气情况保存在首页
      </p>
      <h2>移除城市:</h2>
      <p>如果你不想在首页关注某个城市,可以通过底部的按钮删除它</p>
      <button @click="toggleInfo">关闭</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getIpData, getWeatherData } from '@/api'

const isShow = ref(false)
const needAdd = ref(false)

const toggleInfo = () => {
  isShow.value = !isShow.value
}

const key = ref('e05b2f8dddd45367d027e8f1792cc710')
const ipData = ref([])
const weatherData = ref({})

onMounted(async () => {
  ipData.value = await getIpData(key.value)
  const res = await getWeatherData(ipData.value.adcode, key.value)
  weatherData.value = res.lives[0]
})
</script>

<style lang="scss" scoped>
.weather-header {
  position: sticky;
  top: 0;
  width: 100vw;
  height: 80px;
  background-color: #00668a;
  color: white;
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  z-index: 10;

  .container {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto;
    padding: 24px 160px;
    gap: 16px;
    height: 100%;
    max-width: 1280px;
    font-weight: 600;

    a {
      color: white;
      text-decoration: none;

      .title {
        display: flex;
        align-items: center;
        gap: 12px;
        font-size: 24px;
      }
    }

    .city {
      display: flex;
      flex: 1;
      align-items: center;
      gap: 16px;

      h4 {
        font-size: 18px;
        font-weight: 400;
        margin: 0;
      }

      span {
        font-size: 14px;
        font-weight: 400;
      }
    }

    .icon {
      flex: 1 1 0%;
      display: flex;
      justify-content: flex-end;
      gap: 8px;
      font-size: 20px;
      cursor: pointer;
    }
  }

  .backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 20;
  }

  .backdrop.active {
    opacity: 1;
    pointer-events: auto;
  }

  .info-card {
    position: fixed;
    color: #333;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    width: 330px;
    height: 300px;
    background-color: white;
    padding: 16px;
    opacity: 0;
    pointer-events: none;
    transition: all 1s ease;
    z-index: 100;

    h2 {
      font-size: 18px;
      font-weight: 500;
    }
    p {
      margin-bottom: 16px;
      font-size: 12px;
    }

    button {
      margin-top: 10px;
      padding: 8px 24px;
      background-color: #00668a;
      outline: none;
      border: none;
      color: white;
      transition: background-color 0.2s;
      cursor: pointer;
    }
  }

  .is-show {
    display: block;
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
    pointer-events: auto;
  }

  .add {
    display: none;
  }
}
</style>
